//引入"UserDao"依赖
const UserDao = require("../../dao/user/user_dao.js");

const UserService = {
	//登录
	login(req, res, next) {
		const { //使用解构赋值
			username,
			password
		} = req.body;
		//数据访问
		UserDao.find({
				username
			})
			.then((data) => {
				if(data.length === 1) { //存在与查找用户名相关的对象
					//判断密码
					if(password === data[0].password) { //密码一致
						//用户登录成功
						req.session.loginUser = username;
						res.json({
							res_code: 1,
							res_error: "",
							res_body: {
								status: 1,
								message: "登陆成功",
								data: {
									username: data[0].username
								}
							}
						});
					} else { //密码不一致
						res.json({
							res_code: 1,
							res_error: "",
							res_body: {
								status: 0,
								message: "密码错误",
								data: {}
							}
						});
					}
				} else { //不存在存在与查找用户名相关的对象
					res.json({
						res_code: 1,
						res_error: "",
						res_body: {
							status: 0,
							message: "用户名不存在",
							data: {}
						}
					});
				}
			})
			.catch((err) => {
				res.json({
					res_code: 1,
					res_error: "",
					res_body: {}
				});
			});
	},

	//注册
	register(req, res, next) {
		//从请求中获取用户名，密码，邮箱地址 
		//GET
		/*const {
		  username,
		  password,
		  email
		} = req.query;*/
		//POST
		const { //使用解构赋值
			username,
			password
		} = req.body;
		//密码加密

		//将用户注册信息发送到数据访问层
		UserDao.save({ //这里是对象简写
				username,
				password
			})
			.then((data) => {
				//注册成功即登录成功
				req.session.loginUser = username;
				res.json({
					res_code: 1,
					res_error: "",
					res_body: {
						status: 1,
						message: "success",
						data: {
							username: data.username
						}
					}
				});
			})
			.catch((err) => {
				res.json({
					res_code: 0,
					res_error: "",
					res_body: {
						status: 0,
						message: "faild" + err,
						data: {}
					}
				});
			});
	},

	//检测
	check(req, res, next) {
		const {
			page
		} = req.query;
		//查询指定页码数据
		UserDao.findByPage(page)
			.then((data) => {
				res.json({
					res_code: 1,
					res_error: "",
					res_body: {
						status: 1,
						list: data
					}
				});
			})
			.catch((err) => {
				res.json({
					res_code: 0,
					res_error: err,
					res_body: {}
				});
			});

	},
	//注销
	logout(req, res, next) {
		//将session记录改变(清空)
		req.session.loginUser = null;
		res.json({
			res_code: 1,
			res_eror: "",
			res_body: {
				status: 1
			}
		});
	},
	//删除
	deleteById(req, res, next) {
		const {
			id
		} = req.query;
		UserDao.deleteById(id)
			.then((data) => {
				res.json({
					res_code: 1,
					res_error: "",
					res_body: {}
				});
			})
			.catch((err) => {
				res.json({
					res_code: 0,
					res_error: err,
					res_body: {}
				});
			});
	},
	updateById(req, res, next) {		
		let logo = "";
		if(req.file) {
			logo = "/images/upload/" + req.file.filename;
		}
		const {
			id,
			username,
			password
		} = req.query;
		UserDao.updateById({
				id,
				username,
				password
			})
			.then((data) => {
				console.log(data);
				res.json({
					res_code: 1,
					res_error: "",
					res_body: {
						status: 1
					}
				});
			})
			.catch((err) => {
				res.json({
					res_code: 0,
					res_error: err,
					res_body: {
						status: 0
					}
				});
			});
	}
}

module.exports = UserService;